import codec.CommandDecoder;
import codec.CommandEncoder;
import command.SetCommand;
import filter.AppendFilter;
import handler.MemcachedHandler;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URLEncoder;

/**
 * 单个memcached客户度
 * User: zhangyong
 * Date: 13-7-23
 * Time: 下午9:49
 * To change this template use File | Settings | File Templates.
 */
public class SingleMemcachedClient {

    private IoSession ioSession;

    public SingleMemcachedClient(String host, int port) {
        IoConnector connector = new NioSocketConnector();
        //设置超时时间
        connector.setConnectTimeoutMillis(3000L);

        ProtocolCodecFactory factory = new ProtocolCodecFactory() {
            private ProtocolDecoder decoder = new CommandDecoder();
            private ProtocolEncoder encoder = new CommandEncoder();

            public ProtocolEncoder getEncoder(IoSession session) throws Exception {
                return encoder;
            }

            public ProtocolDecoder getDecoder(IoSession session) throws Exception {
                return decoder;
            }
        };
        connector.setHandler(new MemcachedHandler());
        connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(factory));
        connector.getFilterChain().addLast("append", new AppendFilter());
        try {
            ConnectFuture future = connector.connect(new InetSocketAddress(host, port));
            future.awaitUninterruptibly(); //等待连接创建完成
            ioSession = future.getSession();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Object get(String key) {
        return null;
    }

    public void set(String key, Object value, int expireTime) throws UnsupportedEncodingException {
        SetCommand setCommand = new SetCommand(sanitizeKey(key), value, expireTime);
        ioSession.write(setCommand);
    }

    private String sanitizeKey(String key) throws UnsupportedEncodingException {
        return URLEncoder.encode(key, "UTF-8");
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String host = "192.168.5.119";
        int port = 12000;
        SingleMemcachedClient singleMemcachedClient = new SingleMemcachedClient(host, port);
        //    Object object = singleMemcachedClient.get("hello");
        //    System.out.println(object);
        singleMemcachedClient.set("hello", "1", 0);
    }

}
